package com.yuandian.management.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.business_monitor.feign.RemoteElusiveIndicatorsService;
import com.yuandian.api.management.entity.ConfExclusiveIndicator;
import com.yuandian.api.management.entity.ConfResource;
import com.yuandian.api.management.vo.ExclusiveIndicatorsExcelVo;
import com.yuandian.api.management.vo.IndicatorImportVo;
import com.yuandian.bpm.admin.api.entity.SysDictItem;
import com.yuandian.bpm.admin.api.feign.RemoteDictService;
import com.yuandian.bpm.admin.api.feign.RemoteParamService;
import com.yuandian.bpm.common.core.constant.SecurityConstants;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.constant.ConfigKey;
import com.yuandian.constant.Constants;
import com.yuandian.constant.DictType;
import com.yuandian.management.mapper.ConfExclusiveIndicatorMapper;
import com.yuandian.management.service.ConfExclusiveIndicatorService;
import com.yuandian.management.service.ConfResourceService;
import com.yuandian.management.service.ConfStepService;
import com.yuandian.management.utils.DisplayFieldUtil;
import com.yuandian.management.vo.ExclusiveIndicatorVo;
import com.yuandian.utils.BeanUtils;
import com.yuandian.utils.ListUtils;
import com.yuandian.utils.StringUtils;
import com.yuandian.utils.poi.BatchDataResult;
import com.yuandian.utils.poi.ExcelService;
import com.yuandian.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 迁移至bpm3.0
 */
@Service
@Slf4j
public class ConfExclusiveIndicatorServiceImpl extends ServiceImpl<ConfExclusiveIndicatorMapper, ConfExclusiveIndicator>
		implements ConfExclusiveIndicatorService {

	@Autowired
	RemoteDictService remoteDictService;

	@Lazy
	@Autowired
	ConfResourceService confResourceService;


	@Autowired
	ConfExclusiveIndicatorMapper confExclusiveIndicatorMapper;

	@Lazy
	@Autowired
	ConfStepService confStepService;

	@Autowired
	private RemoteElusiveIndicatorsService remoteElusiveIndicatorsService;

	@Autowired
	private RemoteParamService remoteParamService;


	/**
	 * 指标映射关系
	 */
	private static Map<String, String> effectMapping = new HashMap<>();
	static {
		effectMapping.put("1", "是");
		effectMapping.put("0", "否");
		effectMapping.put("是", "1");
		effectMapping.put("否", "0");
		effectMapping.put("relevance_0", "非关联指标");
		effectMapping.put("relevance_1", "是前端关联指标");
		effectMapping.put("relevance_2", "是后端关联指标");
		effectMapping.put("relevance_3", "是前后端关联指标");
		effectMapping.put("非关联指标", "0");
		effectMapping.put("是前端关联指标", "1");
		effectMapping.put("是后端关联指标", "2");
		effectMapping.put("是前后端关联指标", "3");
		effectMapping.put("decode_0", "否");
		effectMapping.put("decode_1", "Base64解码");
		effectMapping.put("decode_2", "Unicode解码");
		effectMapping.put("decode_3", "url解码");
		effectMapping.put("decode_4", "url+unicode解码");
		effectMapping.put("Base64解码", "1");
		effectMapping.put("Unicode解码", "2");
		effectMapping.put("url解码", "3");
		effectMapping.put("url+unicode解码", "4");
	}
	/**
	 * 下载专属指标导入模板
	 *
	 * @param response 响应上下文
	 */
	@Override
	public void importTemplate(HttpServletResponse response) throws IOException {
		// 1、设置response
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		String fileName = URLEncoder.encode("专属指标导入模板", "utf-8").replaceAll("\\+", "%20");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

		InputStream is = null;
		OutputStream os = null;
		try {
			// 2、获取文件输入流
			Resource resource = new ClassPathResource(Constants.IMPORT_INDICATOR_TEMPLATE_PATH);
			is = resource.getInputStream();

			// 3、获取输出流
			os = new BufferedOutputStream(response.getOutputStream());

			// 4、写入
			int len;
			byte[] bytes = new byte[1024];
			while ((len = is.read(bytes)) != -1) {
				os.write(bytes, 0, len);
			}
			os.flush();
		} finally {
			// 5、关闭资源
			if (is != null) {
				is.close();
			}
			if (os != null) {
				os.close();
			}
		}
	}


	/**
	 * 导出(下载)专属指标列表
	 *
	 * @param exclusiveIndicator 专属指标查询参数
	 * @param response
	 */
	@Override
	public void downloadExclusiveIndicators(ConfExclusiveIndicator exclusiveIndicator, HttpServletResponse response) throws IOException {
		if (CollectionUtils.isEmpty(exclusiveIndicator.getResourceCodes())) {
			response.reset();
			response.setContentType(Constants.CONTENT_TYPE_APPLICATION_JSON);
			response.setCharacterEncoding("utf-8");
			response.getWriter().println(JSONUtil.parse(R.failed("资源编码列表不能为空")));
			return;
		}

		// 校验服务资源是否存在
		boolean checkFlag = getCheckFlag(exclusiveIndicator);

		if (!checkFlag) {
			response.reset();
			response.setContentType(Constants.CONTENT_TYPE_APPLICATION_JSON);
			response.setCharacterEncoding("utf-8");
			response.getWriter().println(JSONUtil.parse(R.failed("该服务资源不存在，请重新刷新页面！")));
			return;
		}

		ConfExclusiveIndicator confExclusiveIndicator = new ConfExclusiveIndicator();
		confExclusiveIndicator.setResourceCodes(exclusiveIndicator.getResourceCodes());
		confExclusiveIndicator.setOrderByColumn(exclusiveIndicator.getOrderByColumn());
		confExclusiveIndicator.setIsAsc(exclusiveIndicator.getIsAsc());

		List<ConfExclusiveIndicator> eiVoList = confExclusiveIndicatorMapper.list(confExclusiveIndicator);
		List<ExclusiveIndicatorsExcelVo> list = new ArrayList<>();
		eiVoList.forEach(ei -> {
			ExclusiveIndicatorsExcelVo vo = BeanUtils.convert(ei, ExclusiveIndicatorsExcelVo.class);
			Map map = JSONUtil.parseObj(ei.getDetail()).toBean(Map.class);
			vo.setIsShow(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isShow")).orElse(0))));
			vo.setIsDeal(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isDeal")).orElse(0))));
			vo.setIsUser(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isUserFlag")).orElse(0))));
			vo.setIsChannel(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isChannel")).orElse(0))));
			vo.setIsRegion(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isRegion")).orElse(0))));
			vo.setIsCusReCode(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isCustomReqCode")).orElse(0))));
			vo.setIsBusinessHallCode(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isBusinessHallCode")).orElse(0))));
			vo.setIsOpIdCode(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isOperatorCode")).orElse(0))));
			vo.setIsPhoneCode(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isPhoneNumber")).orElse(0))));
			vo.setIsSaveOri(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isRetainOri")).orElse(0))));
			vo.setIsCheckAll(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isFullExtraction")).orElse(0))));
			vo.setIsBackfill(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isBackFill")).orElse(0))));
			vo.setIsSensitiveInformation(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isShowSensitive")).orElse(0))));
			vo.setIsExportSensitive(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isExposeSensitive")).orElse(0))));
			vo.setIsShowSensitive(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isShowSensitive")).orElse(0))));
			vo.setIsResultExplain(effectMapping.get(String.valueOf(Optional.ofNullable(map.get("isAlarm")).orElse(0))));
			vo.setIsRelevance(effectMapping.get("relevance_" + Optional.ofNullable(map.get("relevanceType")).orElse(0)));
			vo.setIsDecode(effectMapping.get("decode_" + Optional.ofNullable(map.get("decodeType")).orElse(0)));
			vo.setSensitiveLength(String.valueOf(Optional.ofNullable(map.get("sensitiveLength")).orElse(0)));
			vo.setSensitiveStartValue(String.valueOf(Optional.ofNullable(map.get("sensitiveStartValue")).orElse(0)));
			list.add(vo);
		});
		if (CollectionUtils.isEmpty(list)) {
			list.add(new ExclusiveIndicatorsExcelVo());
		}
		ExcelUtil.writeExcel(ExclusiveIndicatorsExcelVo.class, "专属指标列表", "专属指标列表", list, response);
	}

	private boolean getCheckFlag (ConfExclusiveIndicator exclusiveIndicator) {
		boolean checkFlag = true;
		List<String> resourceCodes = exclusiveIndicator.getResourceCodes();
		if (!CollectionUtils.isEmpty(resourceCodes)) {
			for (String resourceCode : resourceCodes) {
				ConfResource confResource = confResourceService.getOne(new LambdaQueryWrapper<ConfResource>().eq(ConfResource::getResourceCode, resourceCode));
				if (confResource == null) {
					checkFlag = false;
					break;
				}
			}
		} else {
			ConfResource confResource = confResourceService.getOne(new LambdaQueryWrapper<ConfResource>().eq(ConfResource::getResourceCode, exclusiveIndicator.getResourceCode()));
			if (confResource == null) {
				checkFlag = false;
			}
		}

		return checkFlag;
	}

	@Override
	public boolean save(ConfExclusiveIndicator confExclusiveIndicator) {
		JSONObject jsonObject = JSONUtil.createObj()
				.put("isShow", confExclusiveIndicator.getIsShow())
				.put("isDeal", confExclusiveIndicator.getIsDeal())
				.put("isUserFlag", confExclusiveIndicator.getIsUserFlag())
				.put("isChannel", confExclusiveIndicator.getIsChannel())
				.put("isRegion", confExclusiveIndicator.getIsRegion())
				.put("isCustomReqCode", confExclusiveIndicator.getIsCustomReqCode())
				.put("isBusinessHallCode", confExclusiveIndicator.getIsBusinessHallCode())
				.put("isOperatorCode", confExclusiveIndicator.getIsOperatorCode())
				.put("isPhoneNumber", confExclusiveIndicator.getIsPhoneNumber())
				.put("isAlarm", confExclusiveIndicator.getIsAlarm())
				.put("isBackFill", confExclusiveIndicator.getIsBackFill())
				.put("relevanceType", confExclusiveIndicator.getRelevanceType())
				.put("isShowSensitive", confExclusiveIndicator.getIsShowSensitive())
				.put("isExposeSensitive", confExclusiveIndicator.getIsExposeSensitive())
				.put("decodeType", confExclusiveIndicator.getDecodeType())
				.put("isRetainOri", confExclusiveIndicator.getIsRetainOri())
				.put("isFullExtraction", confExclusiveIndicator.getIsFullExtraction())
				.put("decodeType", confExclusiveIndicator.getDecodeType())
				.put("sensitiveStartValue", confExclusiveIndicator.getSensitiveStartValue())
				.put("sensitiveLength", confExclusiveIndicator.getSensitiveLength())
				.put("isSensitiveInformation", confExclusiveIndicator.getIsSensitiveInformation());
		confExclusiveIndicator.setDetail(jsonObject.toString());
		confExclusiveIndicator.setEiCode(StringUtils.generate16Md5(confExclusiveIndicator.getName() + "|" + confExclusiveIndicator.getSource() + "|"
				+ confExclusiveIndicator.getKeyword() + "|" + confExclusiveIndicator.getMatchRegular()));
		//防止 keyword 为空报异常
		confExclusiveIndicator.setKeywordType(Optional.ofNullable(confExclusiveIndicator.getKeywordType()).orElse(" "));
		return super.save(confExclusiveIndicator);
	}

	/**
	 * 分页查询
	 *
	 * @param confExclusiveIndicator 筛选条件
	 * @return 通用返回对象
	 */
	@Override
	public Page<ExclusiveIndicatorVo> listPage(ConfExclusiveIndicator confExclusiveIndicator) {
		Page<ExclusiveIndicatorVo> confExclusiveIndicatorPage = confExclusiveIndicatorMapper.listPage(new Page<>(confExclusiveIndicator.getPageNum(), confExclusiveIndicator.getPageSize()), confExclusiveIndicator);

		return confExclusiveIndicatorPage;
	}

	@Override
	public List<ConfExclusiveIndicator> selectByResourceCodes(List<String> list) {
		return confExclusiveIndicatorMapper.selectByResourceCodes(list);
	}

	/**
	 * 组装需要展示的字段(需求886-20220510)
	 *
	 * @return
	 */
	@Override
	public Map<String, Object> assemblyDisplayField(Map<String, Object> map) {
		if (map == null) {
			map = new HashMap<>();
		}
		Map<String, Object> customMap = new LinkedHashMap<>();
		List<SysDictItem> mapList = new ArrayList<>();

		try {
			long sTime = System.currentTimeMillis();
			mapList = remoteDictService.getDictByType(DictType.CUSTOM_EXCLUSIVE_INDICATOR).getData();
			log.info("[配置系统]-[远程调用-基础服务模块]-[获取公共参数配置]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException("[配置系统]-[远程调用-基础服务模块]-[获取公共参数配置]报错: {}" + e.getMessage());
		}

		if (ListUtils.isNotNull(mapList)) {
			customMap = mapList.stream().collect(Collectors.toMap(SysDictItem::getItemValue, SysDictItem::getLabel));
		}

		if (customMap.size() > 0) {
			map.put("exclusiveIndicator", customMap);
		}

		return map;
	}

	@Override
	public Map<String, Object> queryCustomDisplayFields(String type) {
		Map<String, Object> map = new LinkedHashMap<>();
		switch (type) {
			case "bpmAlarm" :
				map = DisplayFieldUtil.bpmCustomMap;
				break;
			case "apmAlarm" :
				map = DisplayFieldUtil.apmCustomMap;
				break;
			case "detail" :
				map = DisplayFieldUtil.detailCustomMap;
				break;
			case "step" :
				map = DisplayFieldUtil.stepCustomMap;
				String name = remoteParamService.getByKey(ConfigKey.OFFICE_NAME_CONFIGURATION , SecurityConstants.FROM_IN).getData();
				map.put("businessHall", name + "编码");
				map.put("businessHallName", name);
				break;
		}

		return this.assemblyDisplayField(map);
	}

	/**
	 * 组装需要展示的字段(需求886-20220510)
	 *
	 * @return
	 */
	@Override
	public Map<String, Object> assemblyDisplayFieldInner(Map<String, Object> map) {
		if (map == null) {
			map = new HashMap<>();
		}
		Map<String, Object> customMap = new LinkedHashMap<>();
		List<SysDictItem> mapList = new ArrayList<>();

		try {
			long sTime = System.currentTimeMillis();
			mapList = remoteDictService.getDictByType(DictType.CUSTOM_EXCLUSIVE_INDICATOR, SecurityConstants.FROM_IN).getData();
			log.info("[配置系统]-[远程调用-基础服务模块]-[获取公共参数配置]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			throw new RuntimeException("[配置系统]-[远程调用-基础服务模块]-[获取公共参数配置]报错: {}" + e.getMessage());
		}

		if (ListUtils.isNotNull(mapList)) {
			for (SysDictItem sysDictItem : mapList) {
				customMap.put(sysDictItem.getItemValue(), sysDictItem.getLabel());
			}
		}

		if (customMap.size() > 0) {
			map.put("exclusiveIndicator", customMap);
		}

		return map;
	}

	/**
	 * 通过主键查询数据
	 *
	 * @param resourceCode 业务资源
	 * @return 通用返回对象
	 */
	@Override
	public ConfExclusiveIndicator getExclusiveIndicatorByResourceCode(String resourceCode) {
		ConfExclusiveIndicator confExclusiveIndicator = confExclusiveIndicatorMapper.getExclusiveIndicatorByResourceCode(resourceCode);
		return confExclusiveIndicator;
	}

	@Override
	public R<String> updateByResourceCode(ConfExclusiveIndicator confExclusiveIndicator) {
		confExclusiveIndicator.setEiCode(StringUtils.generate16Md5(confExclusiveIndicator.getName() + "|" + confExclusiveIndicator.getSource() + "|"
				+ confExclusiveIndicator.getKeyword() + "|" + confExclusiveIndicator.getMatchRegular()));
		confExclusiveIndicatorMapper.updateByResourceCode(confExclusiveIndicator);
		return R.ok();
	}

	/**
	 * 根据业务资源获取系统,业务名称，步骤名称信息
	 *
	 * @param confExclusiveIndicator
	 * @return
	 */
	@Override
	public Page<Map<String, String>> getSystemAndBusinessAndStepNameByResourceCode(ConfExclusiveIndicator confExclusiveIndicator) {
		return confExclusiveIndicatorMapper.getSystemAndBusinessAndStepNameByResourceCode(new Page<>(confExclusiveIndicator.getPageNum(), confExclusiveIndicator.getPageSize()), confExclusiveIndicator);
	}

	/**
	 * 根据业务资源查询网端信息
	 *
	 * @param confExclusiveIndicator
	 */
	@Override
	public Page<Map<String, String>> getSegmentInfoByResourceCode(ConfExclusiveIndicator confExclusiveIndicator) {
		Map<String, String> segmentTypeAndNameMap = remoteDictService.getDictByType(DictType.SEGMENT_TYPE).getData().stream().collect(Collectors.toMap(SysDictItem::getItemValue, SysDictItem::getLabel));
		Page<Map<String, String>> page = confExclusiveIndicatorMapper.getSegmentInfoByResourceCode(new Page<>(confExclusiveIndicator.getPageNum(), confExclusiveIndicator.getPageSize()), confExclusiveIndicator);
		for (Map<String, String> record : page.getRecords()) {
			record.put("segmentTypeName", segmentTypeAndNameMap.get(record.get("segmentType")));
		}
		return page;
	}

	/**
	 * 批量多资源导入专属指标
	 *
	 * @param file          excel文件
	 * @param resourceCodes
	 * @return 通用返回对象
	 */
	@Override
	public R importExcel(MultipartFile file, List<String> resourceCodes) throws IOException {
		QueryWrapper<ConfResource> resourceQueryWrapper = new QueryWrapper<>();
		List<String> collect = resourceCodes.stream().distinct().filter(e -> e !=null && e !="").collect(Collectors.toList());
		resourceQueryWrapper.in("resource_code", collect);
		List<ConfResource> confResourceList = confResourceService.list(resourceQueryWrapper);
		if (confResourceList.size() != collect.size()) {
//			return R.failed("不存在该服务资源，请重新刷新页面！");
		}

		Map<String, List<IndicatorImportVo>> importRestult = ExcelUtil.readAndSaveExcel(
				file, 0, IndicatorImportVo.class, new ExcelService<IndicatorImportVo>() {
					@Override
					public void saveBatchExcelData(List<IndicatorImportVo> dataList) {
						List<ConfExclusiveIndicator> tempList = new ArrayList<>();
						for (String resourceCode : resourceCodes) {
							dataList.forEach(data -> {
								ConfExclusiveIndicator confExclusiveIndicator = new ConfExclusiveIndicator();
								confExclusiveIndicator.setResourceCode(resourceCode);
								confExclusiveIndicator.setCreateBy(SecurityUtils.getUser().getName());
								confExclusiveIndicator.setCreateTime(LocalDateTime.now());
								confExclusiveIndicator.setName(data.getName());
								confExclusiveIndicator.setSource(data.getSource());
								confExclusiveIndicator.setMatchRegular(data.getMatchRegular());
								confExclusiveIndicator.setKeyword(data.getKeyword());
								confExclusiveIndicator.setKeywordType(data.getKeywordType());
								confExclusiveIndicator.setEiCode(StringUtils.generate16Md5(confExclusiveIndicator.getName() + "|" + confExclusiveIndicator.getSource() + "|"
										+ confExclusiveIndicator.getKeyword() + "|" + confExclusiveIndicator.getMatchRegular()));

								JSONObject json = JSONUtil.createObj();
//								json.set("effect", buildEffect(data));
								json.set("isShow", Optional.ofNullable(effectMapping.get(data.getIsShow())).orElse("0"));
								// 是否交易标识
								json.set("isDeal", Optional.ofNullable(effectMapping.get(data.getIsDeal())).orElse("0"));
								// 关联指标类型
								json.set("relevanceType", data.getRelevanceType() == null ? 0 : effectMapping.get(data.getRelevanceType()));
								// 是否敏感信息
								json.set("isSensitiveInformation", effectMapping.get(data.getIsSensitiveInformation()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsSensitiveInformation())));
								// 展示是否脱敏
								json.set("isShowSensitive", effectMapping.get(data.getIsShowSensitive()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsShowSensitive())));
								// 导出是否脱敏
								json.set("isExportSensitive", effectMapping.get(data.getIsExportSensitive()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsExportSensitive())));
								// 是否全量拼接
								json.set("isFullExtraction", effectMapping.get(data.getIsFullExtraction()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsFullExtraction())));
								// 是否保留原始数据
								json.set("isRetainOri", effectMapping.get(data.getIsRetainOri()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsRetainOri())));
								json.set("isUserFlag", effectMapping.get(data.getIsUser()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsUser())));
								json.set("isRegion", effectMapping.get(data.getIsRegion()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsRegion())));
								json.set("isChannel", effectMapping.get(data.getIsChannel()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsChannel())));
								json.set("isCustomReqCode", effectMapping.get(data.getIsCustomReqCode()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsCustomReqCode())));
								json.set("isBusinessHallCode", effectMapping.get(data.getIsBusinessHallCode()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsBusinessHallCode())));
								json.set("isOperatorCode", effectMapping.get(data.getIsOperatorCode()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsOperatorCode())));
								json.set("isPhoneNumber", effectMapping.get(data.getIsPhoneNumber()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsPhoneNumber())));
								json.set("decodeType", effectMapping.get(data.getDecodeType()) == null ? 0 : effectMapping.get(data.getDecodeType()));
								json.set("isBackFill", effectMapping.get(data.getIsBackfill()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsBackfill())));
								json.set("isAlarm", effectMapping.get(data.getIsAlarm()) == null ? 0 : Integer.valueOf(effectMapping.get(data.getIsAlarm())));
								//如果要脱敏，但未设置脱敏长度和脱敏开始，则设置为1
								if (Integer.valueOf(String.valueOf(json.get("isSensitiveInformation"))) == 1) {
									if (data.getSensitiveLength() == null || "".equals(data.getSensitiveLength())) {
										json.set("sensitiveLength", "1");
									}
									if (data.getSensitiveStartValue() == null || "".equals(data.getSensitiveStartValue())) {
										json.set("sensitiveStartValue", "1");
									}
								}
								confExclusiveIndicator.setDetail(json.toString());
								tempList.add(confExclusiveIndicator);
							});
						}
						saveBatch(tempList, 5000);
					}

					/**
					 * 入库前-批量校验数据
					 *
					 * @param dataList
					 * @return
					 */
					@Override
					public BatchDataResult<IndicatorImportVo> checkBatchExcelData(List<IndicatorImportVo> dataList) {
						return null;
					}



					@Override
					public boolean checkExcelData(IndicatorImportVo data) {
						return !StringUtils.isEmpty(data.getName(), data.getSource(), data.getKeywordType(), data.getMatchRegular());
					}
				});
		List<IndicatorImportVo> successResult = importRestult.get("result");
		List<IndicatorImportVo> failResult = importRestult.get("fail");
		int successLen = successResult.size();
		int failLen = failResult.size();
		if (failLen > 0) {
			log.error("专属指标导入失败项: " + failResult);
		}
		successResult.clear();
		failResult.clear();
		if (successLen == 0 && failLen == 0) {
			return R.failed("导入失败，导入数据内容不能为空");
		}
		return R.ok("导入完成，成功: " + successLen + "条，必填项未填失败：" + failLen + "条");
	}

	@Override
	public void deleteSyncExclusiveIndicators(Map<String, Object> map) {
		confExclusiveIndicatorMapper.deleteSyncExclusiveIndicators(map);
	}


}
